package com.controller;

import com.service.RecordService;
import com.util.AjaxResult;
import com.vo.UserGpInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈忠杰
 * @Date: 2021/06/29/16:59
 * @Description:这是一个关于处理积分和金币的Controller 其中包括的路径有：
 * @PostMapping("/gp/findAllRecords")
 */
@RestController
@RequestMapping("gp")
public class RecordController {

    @Autowired
    RecordService recordService;

    @PostMapping("findAllRecords")
    public AjaxResult findAllRecords(int pageNo) {
        AjaxResult ajaxResult = new AjaxResult();
        UserGpInfoVO allGoldPoints = recordService.getAllGoldPoints(pageNo);
        ajaxResult.setObj(allGoldPoints);
        return ajaxResult;
    }

    @PostMapping("exchangePoints")
    public ModelAndView exchangePoints(int pointscount) {
        ModelAndView modelAndView = new ModelAndView();
        //进一次验证
        UserGpInfoVO allGoldPoints = recordService.getAllGoldPoints(1);
        int sum_point_count = allGoldPoints.getSum_point_count();
        if (pointscount > sum_point_count) {
            modelAndView.setViewName("error/golderror");
            return modelAndView;
        }
        modelAndView.setViewName("front/front_record");
        recordService.insGoldPoints(1, pointscount);
        return modelAndView;
    }
}
